package com.study.controller;

import com.study.domain.User;
import com.study.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;

@Controller
public class LoginController {

    @RequestMapping("toLoginPage")
    public String toGetIds(Model model) {

        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }


    @Autowired
    UserMapper userMapper;

    @RequestMapping("login")
    public String login(HttpServletRequest request, Model model) {
        //把页面填的用户名和密码传过来
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username=" + username);
        System.out.println("password=" + password);

        //登录从数据库中查出来，显示到页面中
        User dbUser = userMapper.loginUser(username, password);

        model.addAttribute("name", dbUser.getName());

        return "index";
    }

}
